const express = require('express');
const router = express.Router();
const CategoryController = require('../controllers/CategoryController');
const authenticate = require('../middleware/auth');

/**
 * @swagger
 * tags:
 *   name: Categories
 *   description: 分类管理
 */

// All routes require authentication
router.use(authenticate);

/**
 * @swagger
 * /api/categories:
 *   get:
 *     summary: 获取用户分类列表
 *     tags: [Categories]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 成功获取分类列表
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.get('/', CategoryController.getCategories);

/**
 * @swagger
 * /api/categories:
 *   post:
 *     summary: 创建新分类
 *     tags: [Categories]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - type
 *             properties:
 *               name:
 *                 type: string
 *                 description: 分类名称
 *               type:
 *                 type: string
 *                 enum: [income, expense]
 *                 description: 分类类型
 *               icon:
 *                 type: string
 *                 description: 图标
 *               color:
 *                 type: string
 *                 description: 颜色代码
 *     responses:
 *       201:
 *         description: 分类创建成功
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       500:
 *         description: 服务器内部错误
 */
router.post('/', CategoryController.createCategory);

/**
 * @swagger
 * /api/categories/{id}:
 *   put:
 *     summary: 更新分类
 *     tags: [Categories]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 分类ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: 分类名称
 *               type:
 *                 type: string
 *                 enum: [income, expense]
 *                 description: 分类类型
 *               icon:
 *                 type: string
 *                 description: 图标
 *               color:
 *                 type: string
 *                 description: 颜色代码
 *     responses:
 *       200:
 *         description: 分类更新成功
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       404:
 *         description: 分类未找到
 *       500:
 *         description: 服务器内部错误
 */
router.put('/:id', CategoryController.updateCategory);

/**
 * @swagger
 * /api/categories/{id}:
 *   delete:
 *     summary: 删除分类
 *     tags: [Categories]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         description: 分类ID
 *     responses:
 *       200:
 *         description: 分类删除成功
 *       400:
 *         description: 无法删除有关联账单的分类
 *       401:
 *         description: 未授权
 *       404:
 *         description: 分类未找到
 *       500:
 *         description: 服务器内部错误
 */
router.delete('/:id', CategoryController.deleteCategory);

module.exports = router;